home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / startrek / trek73 / src / misc.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  5KB  |  266 lines

  1. /*
  2.  * TREK73: misc.c
  3.  *
  4.  * Miscellaneous Routines
  5.  *
  6.  * help, scancmd, new_slot, return_slot, betw, vowelstr, plural,
  7.  * check_p_damage, check_t_damage, check_p_turn, check_t_turn
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include "defines.h"
  13. #include "structs.h"
  14.  
  15. int help(dummy)
  16. struct ship *dummy;
  17. {
  18.     extern    struct cmd cmds[];
  19.     struct    cmd *cp;
  20.     int column = 0;
  21.  
  22.     printf("\nTrek84 Commands: \n");
  23.     printf("Code        Command\n\n");
  24.     for (cp = &cmds[0]; cp->routine != NULL; cp++) {
  25.         printf("%3s: ", cp->word1);
  26.         if (cp->turns == 0)
  27.             printf (" *");
  28.         else
  29.             printf ("  ");
  30.         printf(" %-31s", cp->word2);
  31.         if (column++ & 1)
  32.             puts("");
  33.     }
  34.     printf("\n\n\n * does not use a turn\n");
  35.     dummy = dummy;                /* LINT */
  36. }
  37.  
  38. struct cmd *scancmd(buf)
  39. char *buf;
  40. {
  41.     extern    struct cmd cmds[];
  42.     struct    cmd *cp;
  43.     extern    char **argp;
  44.     int    argnum;
  45.     int    first;
  46.  
  47.     argnum = parsit(buf, &argp);
  48.     first = strlen(argp[0]);
  49.     if (argnum && first) {
  50.         for (cp = &cmds[0]; cp->routine != NULL; cp++) {
  51.             if (strncmp(argp[0], cp->word1, first) == 0)
  52.                 return (cp);
  53.         }
  54.     }
  55.     return (NULL);
  56. }
  57.  
  58. /*
  59.  * This routine handles getting unique identifier numbers for
  60.  * all objects.
  61.  */
  62. new_slot()
  63. {
  64.     extern char slots[];
  65.     extern int shipnum;
  66.     /*
  67.      * This is to make it appear that in a 2-ship duel, for
  68.      * instance, the first object to appear will be numbered
  69.      * as 3.
  70.      */
  71.     int i = shipnum + 2;
  72.  
  73.     while (slots[i] == 'X')
  74.         i++;
  75.     slots[i] = 'X';
  76.     return i;
  77. }
  78.  
  79. /* 
  80.  * This routine handles returning identifiers
  81.  */
  82. return_slot(i)
  83. int i;
  84. {
  85.     extern char slots[];
  86.     
  87.     if (slots[i] != 'X')
  88.         printf("FATAL ERROR - Slot already empty!");
  89.     slots[i] = ' ';
  90. }
  91.  
  92. betw(i, j, k)
  93. int i, j, k;
  94. {
  95.     if ((i > j) && (i < k))
  96.         return(1);
  97.     else
  98.         return(0);
  99. }
  100.  
  101. char *vowelstr(str)
  102. char *str;
  103. {
  104.     switch(*str) {
  105.         case 'a': case 'A':
  106.         case 'e': case 'E':
  107.         case 'i': case 'I':
  108.         case 'o': case 'O':
  109.         case 'u': case 'U':
  110.             return "n";
  111.         default:
  112.             return "";
  113.     }
  114. }
  115.  
  116. char *plural(i)
  117. int i;
  118. {
  119.     if (i != 1)
  120.         return("s");
  121.     else
  122.         return("");
  123. }
  124.  
  125. /*
  126.  * This routine takes an array generated from commands 1, 3, and 5
  127.  * to print out a list of those phasers damaged and unable to
  128.  * either fire, lock, or turn.
  129.  */
  130. check_p_damage(array, sp, string)
  131. int array[];
  132. struct ship *sp;
  133. char *string;
  134. {
  135.     int i, j = 0;
  136.  
  137.     for (i=0; i<4; i++) {
  138.         if ((array[i] != 0) && (sp->phasers[i].status & P_DAMAGED)) {
  139.             if (!j)
  140.                 printf("Computer: Phaser(s) %d", i+1);
  141.             else
  142.                 printf(", %d", i+1);
  143.             j++;
  144.         }
  145.     }
  146.     if (j)
  147.         printf(" damaged and unable to %s.\n", string);
  148. }
  149.  
  150. /*
  151.  * This routine takes an array generated from commands 2, 4, and 6
  152.  * to print out a list of those tubes damaged and unable to either
  153.  * fire, lock, or turn.
  154.  */
  155. check_t_damage(array, sp, string)
  156. int array[];
  157. struct ship *sp;
  158. char *string;
  159. {
  160.     int i, j = 0;
  161.  
  162.     for (i=0; i<6; i++) {
  163.         if ((array[i] != 0) && (sp->tubes[i].status & P_DAMAGED)) {
  164.             if (!j)
  165.                 printf("Computer: Tube(s) %d", i+1);
  166.             else
  167.                 printf(", %d", i+1);
  168.             j++;
  169.         }
  170.     }
  171.     if (j)
  172.         printf(" damaged and unable to %s.\n", string);
  173. }
  174.  
  175. /*
  176.  * This routine checks to see if a phaser is pointing into our
  177.  * blind side
  178.  */
  179. check_p_turn(array, sp, flag)
  180. int array[];
  181. struct ship *sp;
  182. int flag;            /* If 1, came from fire_phasers */
  183. {
  184.     register int i;
  185.     register int j = 0;
  186.     register int k;
  187.     register int bear;
  188.     struct ship *target;
  189.  
  190.     for (i=0; i<4; i++) {
  191.         if (array[i] == 0)
  192.             continue;
  193.         if ((flag) && (!(sp->phasers[i].status & P_FIRING)))
  194.             continue;
  195.         target = sp->phasers[i].target;
  196.         /*
  197.          * This hack is here since when the phaser is locked,
  198.          * the bearing points at the target, whereas when
  199.          * not locked, the bearing is relative to the ship.
  200.          */
  201.         if (target == NULL) {
  202.             bear = sp->phasers[i].bearing + sp->course;
  203.             k = sp->phasers[i].bearing;
  204.         } else {
  205.             bear = bearing(sp->x, target->x, sp->y, target->y);
  206.             k = bear - sp->course;
  207.         }
  208.         k = rectify(k);
  209.         if ((k > 125) && (k < 235) && (!(sp->status & S_ENG))) {
  210.             if (!j)
  211.                 printf("Computer: Phaser(s) %d", i + 1);
  212.             else
  213.                 printf(", %d", i + 1);
  214.             j++;
  215.         }
  216.     }
  217.     if (j)
  218.         printf(" are pointing into our blind side.\n");
  219. }
  220.  
  221. /*
  222.  * This routine checks to see if a tube is turned into
  223.  * our blind side.
  224.  */
  225. check_t_turn(array, sp, flag)
  226. int array[];
  227. struct ship *sp;
  228. int flag;            /* If 1, came from fire_tubes */
  229. {
  230.     register int i;
  231.     register int j = 0;
  232.     register int k;
  233.     register int bear;
  234.     struct ship *target;
  235.  
  236.     for (i=0; i<6; i++) {
  237.         if (array[i] == 0)
  238.             continue;
  239.         if (flag && (!(sp->tubes[i].status & T_FIRING)))
  240.             continue;
  241.         target = sp->tubes[i].target;
  242.         /*
  243.          * This hack is here since when the tube is locked,
  244.          * the bearing points at the target, whereas when
  245.          * not locked, the bearing is relative to the ship.
  246.          */
  247.         if (target == NULL) {
  248.             bear = sp->tubes[i].bearing + sp->course;
  249.             k = sp->tubes[i].bearing;
  250.         } else {
  251.             bear = bearing(sp->x, target->x, sp->y, target->y);
  252.             k = bear - sp->course;
  253.         }
  254.         k = rectify(k);
  255.         if ((k > 135) && (k < 225) && (!(sp->status & S_ENG))) {
  256.             if (!j)
  257.                 printf("Computer: Tubes(s) %d", i + 1);
  258.             else
  259.                 printf(", %d", i + 1);
  260.             j++;
  261.         }
  262.     }
  263.     if (j)
  264.         printf(" are pointing into our blind side.\n");
  265. }
  266.